<template>
	<view class="navSearch">
		<u-navbar :placeholder="true" leftText="" title="添加资格证书" auto-back>
			<template #left>
				<view class="" @click="back">
					<u-icon name="arrow-left" size="22"></u-icon>
				</view>
			</template>
			<template #right>
				<view class="subBtn" @click="editFun">
					完成
				</view>
			</template>
		</u-navbar>
	</view>
	<u-upload
		class="uploadImg"
		:fileList="personInfoData.fileList"
		@afterRead="afterRead($event)"
		@delete="deletePic($event)"
		name="3"
		multiple
		:maxCount="10"
		:previewFullImage="true"
	></u-upload>
</template>

<script setup lang="ts">
	import { ref,reactive,getCurrentInstance} from "vue";
	import {useRouter} from 'uni-mini-router'
	const {proxy} = getCurrentInstance()
	const router = useRouter()
	
	let personInfoData = reactive({
		info:{},
		fileList:[]
	})
	
	// 获取个人简历详情
	const meiyeRecruitHunterInfo2DetailsFun = ()=>{
		//获取详情
		proxy.$req('GET', proxy.$api.meiyeRecruitHunterInfo2Details).then((res) => {
			if(res.code === 200){
				personInfoData.fileList = []
				personInfoData.info = res.result
				if(personInfoData.info.credentials){
					let arr = personInfoData.info.credentials.split(',')
					arr.forEach(el=>{
						personInfoData.fileList.push({url:el})
					})
				}
				
			}
		})
	}
	meiyeRecruitHunterInfo2DetailsFun()
	const back = ()=>{
		router.back()
	}
	// 修改数据
	const editFun = ()=>{
		let imgs = []
		console.log(personInfoData.fileList)
		personInfoData.fileList.forEach(el=>{
			imgs.push(el.url)
		})
		console.log(imgs,'imgsimgsimgsimgs')
		personInfoData.info.credentials = imgs.join(',')
		proxy.$req('POST', proxy.$api.meiyeRecruitHunterInfo2edit,personInfoData.info).then((res) => {
			if(res.code === 200){
				router.back()
			}
		})
	}
	
	const afterRead = (e) => {
		
		const item = e.file[0];
		personInfoData.fileList.push({
			...item,
			//提示上传中
			status: 'uploading',
			message: '上传中'
		});
		uni.uploadFile({
			url: proxy.$uploadUrl + proxy.$api.uploadFile,
			filePath: e.file[0].url,
			name: 'file',
			formData: {
				file: e.file[0].url,
			},
			success: (uploadFileRes) => {
				// console.log(uploadFileRes);
				let result = JSON.parse(uploadFileRes.data).result
				let idx = personInfoData.fileList.length-1
				personInfoData.fileList.splice(
					idx,
					1,
					Object.assign(item, {
						status: 'success',
						message: '上传成功',
						url: result,
					})
				);
			},
			complete() { }
		});
	}
	const deletePic = (e) => {
		personInfoData.fileList.splice(event.index, 1)
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fafafa;
	}

	.navSearch {
		width: 100%;
		border-bottom: 1rpx solid #D7D7D7;
	}

	.subBtn {
		width: 104rpx;
		height: 56rpx;
		line-height: 56rpx;
		text-align: center;
		background: rgba(255, 255, 255, 0);
		border: 1rpx solid rgba(0, 0, 0, 0.25);
		font-size: 24rpx;
		font-weight: 400;
		color: rgba(0, 0, 0, 0.65);
		border-radius: 8rpx;
	}
	.uploadImg{
		padding:20rpx;
	}
</style>